set linesize 140
set rmsg on
set scheme s1mono
/******************************************************************************
	Project		:	The Effects of Army Service in the All-Volunteer Era
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	first-stage.do
	Description	:	This file runs the first stage analysis and creates
					related tables and graphs.
*******************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
*cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"
cap mkdir	"${output}1s/"

*---- select subprograms ----*
local first_stage_regs		1
local first_stage_table		1
local first_stage_graphs	1

local stratalist all black white

*---------------------------------*
*---- First Stage Regressions ----*
*---------------------------------*
if `first_stage_regs'==1{

	*---- initialize output file ----*
	clear
	set obs 1
	foreach strata of local stratalist{
		foreach cut in 31 50{
			foreach v in N m b se F{
				gen `v'_`cut'_`strata'_1s=.
			}
		}
	}
	save "${output}1s/results-1s", replace

	*---- run 1st stage regressions ----*
	foreach strata of local stratalist{
		use access firstafqt *inst* k31* k50* quarterFE `stratalist' using "${data}army-treasury-analysis", clear
		keep if `strata'==1
		
		*1st cutoff
		reg access inst31 k31 k31_2 instk31 instk31_2 ib175.quarterFE if inrange(firstafqt,12,49), r
		local b_31_1s = _b[inst31]
		local se_31_1s = _se[inst31]
		local N_31_1s = e(N)
		sum access if e(sample)
		local m_31_1s = r(mean)
		test inst31 = 0
		local F_31_1s = r(F)

		*2nd cutoff
		reg access inst50 k50 k50_2 instk50 instk50_2 ib175.quarterFE if inrange(firstafqt,31,68), r
		local b_50_1s = _b[inst50]
		local se_50_1s = _se[inst50]
		local N_50_1s = e(N)
		sum access if e(sample)
		local m_50_1s = r(mean)
		test inst50 = 0
		local F_50_1s = r(F)
		
		*write output to file
		use "${output}1s/results-1s", clear
		foreach cut in 31 50{
			foreach v in N m b se F{
				di "`v'_`cut'_1s=``v'_`cut'_1s'"
				replace `v'_`cut'_`strata'_1s = ``v'_`cut'_1s'
			}
		}
		save "${output}1s/results-1s", replace
	}
}	//end if
else di "*---- skipping first stage regressions ----*"

*---------------------------*
*---- First Stage Table ----*
*---------------------------*
if `first_stage_table'==1{
	use "${output}1s/results-1s", clear

	*---- create significance stars ----*
	foreach strata of local stratalist {
		foreach cut in 31 50 {
			if abs(b_`cut'_`strata'_1s/se_`cut'_`strata'_1s) <  1.645	local star_`cut'_`strata' = "" 
			if abs(b_`cut'_`strata'_1s/se_`cut'_`strata'_1s) >= 1.645	local star_`cut'_`strata' = "*" 
			if abs(b_`cut'_`strata'_1s/se_`cut'_`strata'_1s) >= 1.96	local star_`cut'_`strata' = "**" 
			if abs(b_`cut'_`strata'_1s/se_`cut'_`strata'_1s) >= 2.576	local star_`cut'_`strata' = "***" 
		}
	}

	*---- write to table ----*
	file open b using "${output}1s/firststage.tex", write replace
	file write b "\begin{tabular}{l*{6}c}" _n "\hline\hline" _n 
	file write b "& \multicolumn{2}{c}{All} & \multicolumn{2}{c}{Black} & \multicolumn{2}{c}{White} \\" _n
	file write b "& (1) & (2) & (3) & (4) & (5) & (6) \\ \cline{2-7}" _n
	file write b "\textbf{1}(AFQT$\geq$31)" _char(38) %5.3f (b_31_all_1s) "`star31_all'" _char(38) "" _char(38) %5.3f (b_31_black_1s) "`star31_black'" _char(38) "" _char(38) %5.3f (b_31_white_1s) ("`star31_white'") _char(38) "\\" _n	
	file write b "" _char(38) %5.3f _char(40) (se_31_all_1s) _char(41) _char(38) "" _char(38) %5.3f _char(40) (se_31_black_1s) _char(41) _char(38) "" _char(38) %5.3f _char(40) (se_31_white_1s) _char(41) _char(38) "\\" _n
	file write b "\textbf{1}(AFQT$\geq$50)" _char(38) "" _char(38) %5.3f (b_50_all_1s) "`star50_all'" _char(38) "" _char(38) %5.3f (b_50_black_1s) "`star50_black'" _char(38) "" _char(38) %5.3f (b_50_white_1s) "`star50_white'" "\\" _n
	file write b "" _char(38) "" _char(38) _char(40) %5.3f (se_50_all_1s) _char(41) _char(38) "" _char(38) _char(40) %5.3f (se_50_black_1s) _char(41) _char(38) "" _char(38) _char(40) %5.3f (se_50_white_1s) _char(41) "\\" _n
	file write b "\hline" _n
	file write b "Observations" _char(38) %11.0fc (N_31_all_1s) _char(38) %11.0fc (N_50_all_1s) _char(38) %11.0fc (N_31_black_1s) _char(38) %11.0fc (N_50_black_1s) _char(38) %11.0fc (N_31_white_1s) _char(38) %11.0fc (N_50_white_1s) "\\" _n
	file write b "Dep. Var. Mean" _char(38) %5.3f (m_31_all_1s) _char(38) %5.3f (m_50_all_1s) _char(38) %5.3f (m_31_black_1s) _char(38) %5.3f (m_50_black_1s) _char(38) %5.3f (m_31_white_1s) _char(38) %5.3f (m_50_white_1s) "\\" _n
	file write b "F-stat" _char(38) %11.0f (F_31_all_1s) _char(38) %11.0f (F_50_all_1s) _char(38) %11.0f (F_31_black_1s) _char(38) %11.0f (F_50_black_1s)  _char(38) %11.0f (F_31_white_1s) _char(38) %11.0f (F_50_white_1s) "\\" _n
	file write b "\hline \hline" _n "\end{tabular}" _n
	file close b
}	//end if
else di "*---- skipping first stage table ----*"

*----------------------------*
*---- First Stage Graphs ----*
*----------------------------*
if `first_stage_graphs'==1{
	foreach strata of local stratalist{
		use access firstafqt *inst* k31* k50* quarterFE `stratalist' using "${data}army-treasury-analysis", clear
		keep if inrange(firstafqt,12,68) & `strata'==1
		
		collapse (mean) access, by(firstafqt)
		tw	(qfitci access firstafqt if inrange(firstafqt,12,30)	///
			, clc(black) clw(medthick) fc(gs13) alc(white))	///
			(qfitci access firstafqt if inrange(firstafqt,31,49),	///
			clc(black) clw(medthick) fc(gs13) alc(white))	///
			(qfitci access firstafqt if inrange(firstafqt,50,68),	///
			clc(black) clw(medthick) fc(gs13) alc(white))	///
			(scatter access firstafqt if inrange(firstafqt,12,68),	///
			msiz(medsmall) mc(black) mfc(black) mlw(vvthin)),	///
			xtitle("First AFQT Score on File") xlabel(15(5)65) xmtick(12(1)68)	///
			ytitle("Enlisted in Military Service") ylabel(, angle(0) format(%03.1f))	///
			xline(30.5 49.5, lcolor(black) lpattern(dot)) legend(off)
		graph export "${output}1s/first-stage-qfit-access-`strata'.pdf", as(pdf) replace
	}
}	//end if
else di "*---- skip first stage graphs ----*"

snapshot erase _all
